package com.qst.service;

import com.qst.mapper.UserMapper;
import com.qst.pojo.*;
import com.qst.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;


@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> getUserList(UserQuery userQuery) {

        return userMapper.queryAllUser(userQuery);
    }

    public Result login(String userName, String userPwd,Integer roleId, String verityCode, HttpSession session) {

        //非空校验
        if(StringUtils.isEmpty(userName)){

            return new Result(100,"用户名不能为空");

        }
        if(StringUtils.isEmpty(userPwd)){

            return new Result(100,"密码不能为空");

        }
        if(StringUtils.isEmpty(verityCode)){

            return new Result(100,"验证码不能为空");

        }


        //        判断验证码是否正确
//        将画出来放到session中的验证码和用户输入的验证码进行比较
        String realVerityCode = (String) session.getAttribute("verifyCode");

        if(!verityCode.equalsIgnoreCase(realVerityCode)){

            return new Result(100,"验证码不正确");

        }
        //登录,通过用户名和密码来进行登录，通过用户名和密码来进行查询
        User user = userMapper.login(userName,userPwd,roleId);
        if(user==null){
            return new Result(100,"用户名或密码错误");
        }
        //用户名密码正确，登陆成功
        return new Result(200,"登录成功",user);

    }

    public Result register(User user, String verityCode,HttpSession session) {
        int row =0;
        //非空校验
        if(StringUtils.isEmpty(user.getUserName())){

            return new Result(100,"用户名不能为空");

        }
        if(StringUtils.isEmpty(user.getUserPwd())){

            return new Result(100,"密码不能为空");

        }
        if(StringUtils.isEmpty(user.getGender())){

            return new Result(100,"性别不能为空");
        }
        if(StringUtils.isEmpty(verityCode)){

            return new Result(100,"验证码不能为空");

        }
        //        判断验证码是否正确
//        将画出来放到session中的验证码和用户输入的验证码进行比较
        String realVerityCode = (String) session.getAttribute("verifyCode");

        if(!verityCode.equalsIgnoreCase(realVerityCode)){

            return new Result(100,"验证码不正确");

        }

        User user0 = userMapper.queryUserByName(user.getUserName());

        //登录,通过用户名和密码来进行登录，通过用户名和密码来进行查询
        if(user0==null){
            User user1= new User();
            user1.setUserName(user.getUserName());
            user1.setUserPwd(user.getUserPwd());
            user1.setGender(user.getGender());
            user1.setRoleId(user.getRoleId());
         row = userMapper.addUser(user1);
        }
        if(user0!=null){
            return new Result(200,"用户已存在");
        }


        if(row!=1){

            return new Result(100,"用户注册失败");

        }


        //用户名密码正确，登陆成功
        return new Result(200,"注册成功");

    }
//    public Result register(User user,Integer roleId) {
//        int row =0;
//        //非空校验
//        if(StringUtils.isEmpty(user.getUserName())){
//
//            return new Result(100,"用户名不能为空");
//
//        }
//        if(StringUtils.isEmpty(user.getUserPwd())){
//
//            return new Result(100,"密码不能为空");
//
//        }
//        if(StringUtils.isEmpty(user.getGender())){
//
//            return new Result(100,"性别不能为空");
//        }
//
//        User user1 = userMapper.queryUserByName(user.getUserName());
//
//        //登录,通过用户名和密码来进行登录，通过用户名和密码来进行查询
//        if(user1==null){
//            User user2= new User();
//            user2.setUserName(user.getUserName());
//            user2.setUserPwd(user.getUserPwd());
//            user2.setGender(user.getGender());
//            row = userMapper.addUser(user2);
//        }
//        if(user1!=null){
//            return new Result(100,"用户已存在");
//        }
//
//
//        if(row!=1){
//
//            return new Result(100,"添加失败");
//
//        }
//
//
//        //用户名密码正确，登陆成功
//        return new Result(200,"添加成功");
//
//    }
    public List<User> getUserListByParam(String userName){
        return userMapper.getUserListByParam(userName);
    }


    public Result addUser(String userName, String userPwd,Integer gender){
        //非空校验
        if(StringUtils.isEmpty(userName)){

            return new Result(100,"用户名不能为空");

        }
        if(StringUtils.isEmpty(userPwd)){

            return new Result(100,"密码不能为空");

        }
        if(gender==null){

            return new Result(100,"性别不能为空");
        }

      int row =userMapper.insert(userName, userPwd);

        if(row!=1){

            return new Result(100,"用户名或密码错误");

        }

        //用户名密码正确，登陆成功
        return new Result(200,"登录成功");

    }
    public User  getById(Integer id){

        return userMapper.getById(id);
    }

//编辑
    public Result editUser(User user) {

        //非空校验
        if(StringUtils.isEmpty(user.getUserName())){

            return new Result(100,"用户名不能为空");

        }
        if(StringUtils.isEmpty(user.getUserPwd())){

            return new Result(100,"密码不能为空");

        }
        if(StringUtils.isEmpty(user.getGender())){

            return new Result(100,"性别不能为空");
        }

        if(StringUtils.isEmpty(user.getUserAge())){

            return new Result(100,"年龄不能为空");
        }

        if(StringUtils.isEmpty(user.getUserHeight())){

            return new Result(100,"身高不能为空");
        }

        if(StringUtils.isEmpty(user.getUserWeight())){

            return new Result(100,"体重不能为空");
        }

        int row = userMapper.update(user);

        if(row!=1){
            return new Result(100,"更新失败");
        }
        return new Result(200,"更新成功");
    }



    public Result deleteUser(Integer id){
        int row=userMapper.deleteUser(id);
        if(row!=1){
            return  new Result(100,"异常情况");
        }
        return new Result(200,"删除成功");
    }

    public  Result delBatch(Integer[] id){
        if(id==null || id.length==0){
            return new Result(100,"至少选中一条记录");
        }
        int row=userMapper.delBatch(id);
//        有多少id就影响多少行,删除失败
        if(row!=id.length){
            return  new Result(100,"异常情况");
        }
//        删除成功
        return new Result(200,"删除成功");
    }

    public List<Menu> getMenuList01(){
        return userMapper.getMenuList01();
    }
    public List<Menu> getMenuList02(){
        return userMapper.getMenuList02();
    }

    public List<Role> gerRoleList() {
        return userMapper.getRoleList();
    }

    public List<EchartsData> getEchartsData() {
        return userMapper.getEchartsData();
    }


    public List<String> getEchartsDataName() {
        return userMapper.getEchartsDataName();
    }

    public List<String> getEchartsDataValue() {
        return userMapper.getEchartsDataValue();
    }
}



